/*
 * timemanage.c
 *
 *  Created on: 2025.4.3
 *      Author: TracyHenry
 */
/*rtc时间管理代码*/
#include "timemanage.h"
#include <stdio.h>
#include <string.h>
#include "usart.h"
#include "timelist.h"

#define DS1307_ADDRESS 0x68  // DS1307 I2C地址
 
// DS1307寄存器地址
#define DS1307_SECONDS 0x00
#define DS1307_MINUTES 0x01
#define DS1307_HOURS   0x02
#define DS1307_DAY     0x03
#define DS1307_DATE    0x04
#define DS1307_MONTH   0x05
#define DS1307_YEAR    0x06

TimeInfo now_time;

/******************************************************************************
 * FunctionName : DS1307_WriteByte
 * Description  : 写入一个字节到DS1307
 * Parameters   : uint8_t，uint8_t
 * Returns      : void
*******************************************************************************/
void DS1307_WriteByte(uint8_t reg, uint8_t data) 
{
    HAL_I2C_Mem_Write(&hi2c1, DS1307_ADDRESS << 1, reg, I2C_MEMADD_SIZE_8BIT, &data, 1, HAL_MAX_DELAY);
}

/******************************************************************************
 * FunctionName : DS1307_WriteSByte
 * Description  : 连续写入len字节到DS1307
 * Parameters   : uint8_t，uint8_t
 * Returns      : void
*******************************************************************************/
void DS1307_WriteSByte(uint8_t reg, uint8_t* data, uint8_t len) 
{
    HAL_I2C_Mem_Write(&hi2c1, DS1307_ADDRESS << 1, reg, I2C_MEMADD_SIZE_8BIT, data, len, HAL_MAX_DELAY);
}

/******************************************************************************
 * FunctionName : DS1307_WriteByte
 * Description  : 从DS1307读取一个字节
 * Parameters   : uint8_t
 * Returns      : uint8_t
*******************************************************************************/
uint8_t DS1307_ReadByte(uint8_t reg) 
{
    uint8_t data;
    HAL_I2C_Mem_Read(&hi2c1, DS1307_ADDRESS << 1, reg, I2C_MEMADD_SIZE_8BIT, &data, 1, HAL_MAX_DELAY);
    return data;
}

/******************************************************************************
 * FunctionName : DS1307_ReadSByte
 * Description  : 从DS1307读取连续7个字节
 * Parameters   : uint8_t
 * Returns      : uint8_t
*******************************************************************************/
void DS1307_ReadSByte(uint8_t reg, uint8_t len, uint8_t *data) 
{

    HAL_I2C_Mem_Read(&hi2c1, DS1307_ADDRESS << 1, reg, I2C_MEMADD_SIZE_8BIT, data, len, HAL_MAX_DELAY);
}


/******************************************************************************
 * FunctionName : DS1307_SetTime
 * Description  : 设置DS1307的时间
 * Parameters   : uint8_t,uint8_t,uint8_t,uint8_t,uint8_t,uint8_t
 * Returns      : void
*******************************************************************************/
void DS1307_SetTime(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t date, uint8_t month, uint8_t year, uint8_t day) 
{
	uint8_t data[7];
	data[0] = ((seconds / 10) << 4) | (seconds % 10);
	data[1] = ((minutes / 10) << 4) | (minutes % 10);
	data[2] = ((hours / 10) << 4) | (hours % 10);
	data[3] = ((day / 10) << 4) | (day % 10);
	data[4] = ((date / 10) << 4) | (date % 10);
	data[5] = ((month / 10) << 4) | (month % 10);
	data[6] = ((year / 10) << 4) | (year % 10);

	taskENTER_CRITICAL();
	DS1307_WriteSByte(DS1307_SECONDS, data, 7);
	taskEXIT_CRITICAL();
}

/******************************************************************************
 * FunctionName : DS1307_GetTime
 * Description  : 读取DS1307的时间
 * Parameters   : uint8_t *,uint8_t *,uint8_t *,uint8_t *,uint8_t *,uint8_t *
 * Returns      : void
*******************************************************************************/
void DS1307_GetTime(uint8_t *seconds, uint8_t *minutes, uint8_t *hours, uint8_t *date, uint8_t *month, uint8_t *year, uint8_t *day) 
{
    uint8_t data[7];
	DS1307_ReadSByte(DS1307_SECONDS, 7, data);
	
	  *year = (*(data+6) >> 4) * 10 + (*(data+6) & 0x0F);
	  *month = (*(data+5) >> 4) * 10 + (*(data+5) & 0x0F);
	  *date = (*(data+4) >> 4) * 10 + (*(data+4) & 0x0F);
	  *day = (*(data+3) >> 4) * 10 + (*(data+3) & 0x0F);
	  *hours = (*(data+2) >> 4) * 10 + (*(data+2) & 0x0F);
	  *minutes = (*(data+1) >> 4) * 10 + (*(data+1) & 0x0F);
	  *seconds = (*(data) >> 4) * 10 + (*(data) & 0x0F);
}

/******************************************************************************
 * FunctionName : taskTimeManageInit
 * Description  : 任务初始化
 * Parameters   : void
 * Returns      : void
*******************************************************************************/
void taskTimeManageInit(void)
{
		DS1307_GetTime(&now_time.second, &now_time.minute, &now_time.hour, &now_time.date, &now_time.month, &now_time.year, &now_time.day);
}


/******************************************************************************
 * FunctionName : now_time_run
 * Description  : 当前时间运行函数，1000ms调用一次，用于对时间进行计算处理
 * Parameters   : void
 * Returns      : void
*******************************************************************************/
void now_time_run(void)
{
	 static uint16_t count = 0;
	 uint8_t flag_leap_year = 0;
	 count ++;
	 if(count == 30)
	 {
	    count = 0;
		DS1307_GetTime(&now_time.second, &now_time.minute, &now_time.hour, &now_time.date, &now_time.month, &now_time.year, &now_time.day);
	 }
	 else
	 {
		now_time.second++;
		if(now_time.second == 60)
		{
			now_time.second = 0;
			now_time.minute ++;
		}
		
		if(now_time.minute == 60)
		{
			now_time.minute = 0;
			now_time.hour ++;
		}

		if(now_time.hour == 24)
		{
			now_time.hour = 0;
			now_time.date ++;
		}

		if (now_time.year % 4 ==0)
		{
			flag_leap_year = 1;
		}
		

		if((now_time.date == 32 && now_time.month == 1)
			||  (now_time.date == 30 && now_time.month == 2 && flag_leap_year == 1)
			||  (now_time.date == 29 && now_time.month == 2 && flag_leap_year == 0)
			||  (now_time.date == 32 && now_time.month == 3)
			||  (now_time.date == 31 && now_time.month == 4)
			||  (now_time.date == 32 && now_time.month == 5)
			||  (now_time.date == 31 && now_time.month == 6)
			||  (now_time.date == 32 && now_time.month == 7)
			||  (now_time.date == 32 && now_time.month == 8)
			||  (now_time.date == 31 && now_time.month == 9)
			||  (now_time.date== 32 && now_time.month == 10)
			||  (now_time.date == 31 && now_time.month == 11)
			||  (now_time.date == 32 && now_time.month == 12))
		{
			now_time.date = 1;
			now_time.month ++;
		}

		if(now_time.month == 13)
		{
			now_time.month = 1;
			now_time.year ++;
		}
	 }
	printf("second:%d\n",now_time.second);
	printf("minute:%d\n",now_time.minute);
	printf("hour:%d\n",now_time.hour);
	printf("date:%d\n",now_time.date);
	printf("month:%d\n",now_time.month);
	printf("year:%d\n",now_time.year);
	printf("day:%d\n",now_time.day);
}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used 
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void timeManageTask(void const * argument)
{
    
    
   //const  char *msg = "Hello, STM32 USART!\r\n";

  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
//	uint8_t FlagStatus = 0;
  while (1)
  {
    osDelay(1000);

    // 插入示例数据
    // insertNode(&timeTable, createNode(25, 5, 27, 9, 0, 30));
    // insertNode(&timeTable, createNode(25, 5, 27, 9, 0, 31));
    // insertNode(&timeTable, createNode(25, 5, 27, 9, 0, 32));
    // insertNode(&timeTable, createNode(25, 5, 27, 9, 0, 33));
    
    // //printfTimeTable(timeTable);
    //03 01 19 05 1E 0B 30 02
    // // 查询示例
//    TimeTableNode *found = findNode(timeTable, 25, 5, 30, 11, 48);
//    if (found) {
//        printf("find timetable %d-%d-%d %02d:%02d duration:%d\n", 
//               found->year, found->month, found->date, found->hour, found->minute, found->duration);
//    } else {
//        printf("no find\n");
//    }
//    
    
    // // 释放内存
    // freeTimeTable(timeTable);
    now_time_run();

	// 使用printf打印日志
//   printf("System Clock: %lu Hz\n", (unsigned long)HAL_RCC_GetSysClockFreq());
  }
  /* USER CODE END StartDefaultTask */
}

